package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.annotation.PluralsRes;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Clue;
import com.swz.dcrm.model.ClueDetail;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.Schedule;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.home.ClueDetailFragmentArgs;
import com.swz.dcrm.ui.viewmodel.CustomerManagementViewModel;
import com.swz.dcrm.ui.viewmodel.ScheduleViewModel;
import com.swz.dcrm.ui.viewmodel.TodayPlanViewModel;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScheduleDetailFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_customer)
    LinearLayout llCustomer;
    @BindView(R.id.tv_name)
    TextView tvName;

    private Schedule schedule;
    @Inject
    ScheduleViewModel scheduleViewModel;
     @Inject
    TodayPlanViewModel todayPlanViewModel;
    private ClueDetail clueDetail;
    public static ScheduleDetailFragment newInstance() {

        return new ScheduleDetailFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        if(schedule.getCustomerId()!=0){
            todayPlanViewModel.getClueDetail(schedule.getCustomerId())
                    .observe(this, clueDetailBaseResponse -> {
                        if(clueDetailBaseResponse==null){
                            return;
                        }
                        clueDetail=clueDetailBaseResponse.getData();
                        tvName.setText(clueDetail.getBsClueVO().getCusName());
                    });
        }

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_schedule_detail;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        schedule=new Gson().fromJson(ScheduleDetailFragmentArgs.fromBundle(getArguments()).getSchedule(),Schedule.class);
        title.setText(getString(R.string.edit_schedule_title4));
        tvTitle.setText(schedule.getContext());
        tvStartTime.setText(schedule.getStartTime());
        tvEndTime.setText(schedule.getEndTime());
        tvNotice.setText(schedule.getBeforeMinuteRemind()+"分钟前");
        if(schedule.getCustomerId()!=0){
            llCustomer.setVisibility(View.VISIBLE);
        }
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_del)
    public void del(){
        scheduleViewModel.delSchedule(schedule.getId())
                .observe(this, objectBaseResponse -> {
                    if(objectBaseResponse.isSuccess()){
                        backClick();
                    }else {
                        showToast(objectBaseResponse.getMsg());
                    }
                });
    }
    @OnClick(R.id.ll_customer)
    public void go(){

        ClueDetailFragmentArgs.Builder builder=new ClueDetailFragmentArgs.Builder();
        builder.setClueId(new Gson().toJson(clueDetail.getBsClueVO().getId()));
        go(null,R.id.action_scheduleDetailFragment_to_clueDetailFragment,builder.build().toBundle());
    }
}
